/**
 * @File LoginController.java
 * @author Dheeraj Singh
 * @Version 1.0
 * @Date March 25, 2013
 * 
 * Copyright (c) 2011 EdRP, Dayalbagh Educational Institute.
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this  list of conditions and the following disclaimer.
 *
 * Redistribution in binary form must reproducuce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL ETRG OR ITS CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributors: Members of EdRP, Dayalbagh Educational Institute
 */
package in.ac.dei.edrp.purchase.login;

import flex.messaging.FlexContext;
import flex.messaging.FlexSession;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

@Controller
public class LoginController extends MultiActionController{

	@Autowired
	@Qualifier("LoginDaoImpl")
	private LoginDao loginDao;
	
	public List<LoginInfoBean> checkLoginDetails(String userName, String password){
		List<LoginInfoBean> loginList = new ArrayList<LoginInfoBean>();
		LoginInfoBean input = new LoginInfoBean();
		input.setUserName(userName);
		input.setPassword(password);
		input.setApplication("FMS");
		loginList = loginDao.checkLogin(input);
		
		if(loginList.size() > 0){
			
			input.setUniversityCode(loginList.get(0).getUniversityCode());
			
			int maxLoginsAllowed = 2;
			int currentLogins = 0;
			List<LoginInfoBean> loginSessionList = (List<LoginInfoBean>) this.getServletContext().getAttribute("loginSessionList");
			for(int i = 0; i < loginSessionList.size(); i++){
				if(loginSessionList.get(i).getUserName().equalsIgnoreCase(loginList.get(0).getUserName()) && 
						loginSessionList.get(i).getUserRole().equalsIgnoreCase(loginList.get(0).getUserRole())){
					currentLogins++;
				}
			}
			if(currentLogins < maxLoginsAllowed){
				int updateLastLoginCount = loginDao.updateLastLogin(input);
				if(updateLastLoginCount > 0){
					FlexSession session = FlexContext.getFlexSession();
					
					LoginInfoBean loginSessionBean = new LoginInfoBean();
					loginSessionBean.setSessionId(session.getId());
					loginSessionBean.setUserId(loginList.get(0).getUserId());
					loginSessionBean.setUserName(loginList.get(0).getUserName());
					loginSessionBean.setUserRole(loginList.get(0).getUserRole());
					loginSessionBean.setUniversityCode(loginList.get(0).getUniversityCode());
					loginSessionBean.setApplication("FMS");
					
					loginSessionList.add(loginSessionBean);
					this.getServletContext().setAttribute("loginSessionList", loginSessionList);
					System.out.println("Login List Size While Login : " + loginSessionList.size());
					
					session.setAttribute("userName", loginList.get(0).getUserName());
					session.setAttribute("universityCode", loginList.get(0).getUniversityCode());
					session.setAttribute("creatorId", loginList.get(0).getUserId());
					session.setAttribute("modifierId", loginList.get(0).getUserId());
					session.setAttribute("userId", loginList.get(0).getUserId());
					session.setAttribute("userRole", loginList.get(0).getUserRole());
					session.setAttribute("lastLogin", loginList.get(0).getLastLogin());
					session.setAttribute("applicationId", "FMS");
				}
			}else{
				loginList.clear();
			}
		}
		return loginList;
	}
	
	
	public String generateMenu(){
		FlexSession session = FlexContext.getFlexSession();
		LoginInfoBean input = new LoginInfoBean();
		StringWriter menu = new StringWriter();
		input.setUserName(session.getAttribute("userName").toString());
		input.setUserRole(session.getAttribute("userRole").toString());
		input.setUniversityCode(session.getAttribute("universityCode").toString());
		input.setApplication(session.getAttribute("applicationId").toString());
		menu = loginDao.getMenu(input);
		String str = menu.toString();
		return str;
	}
	
	public boolean logoutSession(){
		boolean flag = false;
		FlexSession session = FlexContext.getFlexSession();
		String userName = session.getAttribute("userName").toString();
		String userRole = session.getAttribute("userRole").toString();
		List<LoginInfoBean> loginSessionList = (List<LoginInfoBean>) this.getServletContext().getAttribute("loginSessionList");
		for (int i = 0; i < loginSessionList.size(); i ++){
			if(session.getId().equalsIgnoreCase(loginSessionList.get(i).getSessionId()) && userName.equalsIgnoreCase(loginSessionList.get(i).getUserName())
					&& userRole.equalsIgnoreCase(loginSessionList.get(i).getUserRole())){
				loginSessionList.remove(i);
			}
		}
		System.out.println("Login List Size While Destroying Session : " + loginSessionList.size());
		this.getServletContext().setAttribute("loginSessionList", loginSessionList);
		session.invalidate();
		flag = true;
		return flag;
	}
	
}
